//
//  JsOrientationManager.m
//  jsrtc-ios-sdk
//
//  Created by Gikki Ares on 2021/9/28.
//

#import "JsOrientationManager.h"
#import "JsThreadManager.h"

@implementation JsOrientationManager

static int mi_deviceOrientationObserverCount = 0;
	//默认是竖直的.
static UIInterfaceOrientation _currentOrientation = UIInterfaceOrientationPortrait;
	//返回当前的方向
+ (UIInterfaceOrientation)currentOrientation {
	return _currentOrientation;
}

+ (void)setCurrentOrientation:(UIInterfaceOrientation)orientation viewController:(UIViewController *)vc {
	_currentOrientation = orientation;
}

+ (UIInterfaceOrientationMask)interfaceOrientationMaskForCurrentOrientation {
	return [self interfaceOrientationMaskForOrientation:_currentOrientation];
}

+ (UIInterfaceOrientationMask)interfaceOrientationMaskForOrientation:(UIInterfaceOrientation)orientation {
	switch(orientation) {
		case UIInterfaceOrientationUnknown:
		case UIInterfaceOrientationPortrait:
			return UIInterfaceOrientationMaskPortrait;
		case UIInterfaceOrientationLandscapeLeft:
			return UIInterfaceOrientationMaskLandscapeLeft;
		case UIInterfaceOrientationLandscapeRight:
			return UIInterfaceOrientationMaskLandscapeRight;
		case UIInterfaceOrientationPortraitUpsideDown:
			return UIInterfaceOrientationMaskPortraitUpsideDown;
	}
}
+ (UIDeviceOrientation)getCurrentDeviceOrientation {
	__block UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
	if(orientation == UIDeviceOrientationUnknown) {
		[JsThreadManager runInMainThreadSync:^{
			UIInterfaceOrientation interfaceOrientation = [self getCurrentInterfaceOrientation];
			switch(interfaceOrientation) {
				default:
				case UIInterfaceOrientationPortrait:
					orientation = UIDeviceOrientationPortrait;
					break;
				case UIInterfaceOrientationPortraitUpsideDown:
					orientation = UIDeviceOrientationPortraitUpsideDown;
					break;
				case UIInterfaceOrientationLandscapeRight:
					orientation = UIDeviceOrientationLandscapeLeft;
					break;
				case UIInterfaceOrientationLandscapeLeft:
					orientation = UIDeviceOrientationLandscapeRight;
					break;
			}
		}];
	}
	return orientation;
}

//TODO: 什么情况下是unkown?
+(UIInterfaceOrientation)getCurrentInterfaceOrientation {
	UIInterfaceOrientation interfaceOrientation = UIInterfaceOrientationUnknown;
	if (@available(iOS 13.0, *)) {
		UIWindow * window = [UIApplication sharedApplication].keyWindow;
		UIWindowScene * windowScene = window.windowScene;
		interfaceOrientation = windowScene.interfaceOrientation;
	} else {
			// Fallback on earlier versions
		interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
	}
	return interfaceOrientation;
}

+(NSString *)stringForUIInterfaceOrientation:(UIInterfaceOrientation)o {
	switch(o) {
		case UIInterfaceOrientationUnknown:
			return @"UIInterfaceOrientationUnknown";
		case UIInterfaceOrientationPortrait:
			return @"UIInterfaceOrientationPortrait";
		case UIInterfaceOrientationLandscapeLeft:
			return @"UIInterfaceOrientationLandscapeLeft";
		case UIInterfaceOrientationLandscapeRight:
			return @"UIInterfaceOrientationLandscapeRight";
		case UIInterfaceOrientationPortraitUpsideDown:
			return @"UIInterfaceOrientationPortraitUpsideDown";
	}
}
+(NSString *)stringForDeviceOrientation:(UIDeviceOrientation)o {
	switch(o) {
		case UIDeviceOrientationUnknown:
			return @"UIDeviceOrientationUnknown";
		case UIDeviceOrientationPortrait:
			return @"UIDeviceOrientationPortrait";
		case UIDeviceOrientationFaceUp:
			return @"UIDeviceOrientationFaceUp";
		case UIDeviceOrientationFaceDown:
			return @"UIDeviceOrientationFaceDown";
		case UIDeviceOrientationLandscapeLeft:
			return @"UIDeviceOrientationLandscapeLeft";
		case UIDeviceOrientationLandscapeRight:
			return @"UIDeviceOrientationLandscapeRight";
		case UIDeviceOrientationPortraitUpsideDown:
			return @"UIDeviceOrientationPortraitUpsideDown";
	}
}

	//home键在右边.
+(void)setLandscapeRight:(UIViewController *)vc {
			if (@available(iOS 16.0, *)) {
				[vc setNeedsUpdateOfSupportedInterfaceOrientations];
				NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
				UIWindowScene *scene = [array firstObject];
				UIInterfaceOrientationMask orientation = UIInterfaceOrientationMaskLandscapeRight;
				UIWindowSceneGeometryPreferencesIOS *geometryPreferencesIOS = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:orientation];
				[scene requestGeometryUpdateWithPreferences:geometryPreferencesIOS errorHandler:^(NSError * _Nonnull error) {
					NSLog(@"强制%@错误:%@", @"LandscapeRight");
				}];
			} else {
				UIInterfaceOrientation interfaceOrientation =  UIInterfaceOrientationLandscapeRight;
				NSNumber *orientationTarget = [NSNumber numberWithInteger:interfaceOrientation];
				[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
			}
}

+(id)addInterfaceOrientationDidChangeHandler:(void(^)(UIInterfaceOrientation o))handler {
	id observation = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidChangeStatusBarOrientationNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
		UIInterfaceOrientation o = (UIInterfaceOrientation)[note.userInfo[UIApplicationStatusBarOrientationUserInfoKey] intValue];
		handler(o);
	}];
	return observation;
}
+(void)removeInterfaceOrientationDidChangeHandler:(id)observation {
	[[NSNotificationCenter defaultCenter] removeObserver:observation];
}
+(id)addDeviceOrientationDidChangeHandler:(void(^)(UIDeviceOrientation o))handler {
	mi_deviceOrientationObserverCount++;
	if(mi_deviceOrientationObserverCount == 1){
		[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
	}
	id obj = [[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceOrientationDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
//		note.object;
		UIDeviceOrientation o = [UIDevice currentDevice].orientation;
		handler(o);
	}];
	return obj;
}
+(void)removeDeviceOrientationDidChangeHandler:(id)observation {
	mi_deviceOrientationObserverCount --;
	if(mi_deviceOrientationObserverCount == 0) {
		[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
	}
	[[NSNotificationCenter defaultCenter] removeObserver:observation];
}

@end
